#!/usr/bin/env tclsh

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_execsql_test where-clause-eq {
    select last_name from users where id = 2000;
} {Rodriguez}

do_execsql_test where-clause-eq-string {
    select count(1) from users where last_name = 'Rodriguez';
} {61}

do_execsql_test where-clause-ne {
    select count(1) from users where id != 2000;
} {9999}

do_execsql_test where-clause-gt {
    select count(1) from users where id > 2000;
} {8000}

do_execsql_test where-clause-gte {
    select count(1) from users where id >= 2000;
} {8001}

do_execsql_test where-clause-lt {
    select count(1) from users where id < 2000;
} {1999}

do_execsql_test where-clause-lte {
    select count(1) from users where id <= 2000;
} {2000}

do_execsql_test where-clause-unary-true {
    select count(1) from users where 1;
} {10000}

# not correct? should be 0?
do_execsql_test where-clause-unary-false {
    select count(1) from users where 0;
} {0}

do_execsql_test where-clause-no-table-constant-condition-true {
    select 1 where 1;
} {1}

do_execsql_test where-clause-no-table-constant-condition-identifier-true {
    select 1 where true;
} {1}

do_execsql_test where-clause-no-table-constant-condition-true-2 {
    select 1 where '1';
} {1}

do_execsql_test where-clause-no-table-constant-condition-true-3 {
    select 1 where 6.66;
} {1}

do_execsql_test where-clause-no-table-constant-condition-true-4 {
    select 1 where '6.66';
} {1}

do_execsql_test where-clause-no-table-constant-condition-true-5 {
    select 1 where -1;
} {1}

do_execsql_test where-clause-no-table-constant-condition-true-6 {
    select 1 where '-1';
} {1}

do_execsql_test where-clause-no-table-constant-condition-false {
    select 1 where 0;
} {}

do_execsql_test where-clause-no-table-constant-condition-identifier-false {
    select 1 where false;
} {}

do_execsql_test where-clause-no-table-constant-condition-false-2 {
    select 1 where '0';
} {}

do_execsql_test where-clause-no-table-constant-condition-false-3 {
    select 1 where 0.0;
} {}

do_execsql_test where-clause-no-table-constant-condition-false-4 {
    select 1 where '0.0';
} {}

do_execsql_test where-clause-no-table-constant-condition-false-5 {
    select 1 where -0.0;
} {}

do_execsql_test where-clause-no-table-constant-condition-false-6 {
    select 1 where '-0.0';
} {}

do_execsql_test where-clause-no-table-constant-condition-false-7 {
    select 1 where 'hamburger';
} {}

# this test functions as an assertion that the index on users.age is being used, since the results are ordered by age without an order by.
do_execsql_test select-where-and {
    select first_name, age from users where first_name = 'Jamie' and age > 80
} {Jamie|87
Jamie|88
Jamie|88
Jamie|92
Jamie|94
Jamie|99
}

do_execsql_test select-where-or {
    select first_name, age from users where first_name = 'Jamie' and age > 80
} {Jamie|87
Jamie|88
Jamie|88
Jamie|92
Jamie|94
Jamie|99
}

do_execsql_test select-where-and-or {
    select first_name, age from users where first_name = 'Jamie' or age = 1 and age = 2
} {Jamie|94
Jamie|88
Jamie|31
Jamie|26
Jamie|71
Jamie|50
Jamie|28
Jamie|46
Jamie|17
Jamie|64
Jamie|76
Jamie|99
Jamie|92
Jamie|47
Jamie|27
Jamie|54
Jamie|47
Jamie|15
Jamie|12
Jamie|71
Jamie|87
Jamie|34
Jamie|88
Jamie|41
Jamie|73
}

do_execsql_test where-float-int {
    select * from products where price > 50 and name != 'hat';
} {2|cap|82.0
5|sweatshirt|74.0
6|shorts|70.0
7|jeans|78.0
8|sneakers|82.0
11|accessories|81.0}

do_execsql_test where-multiple-and {
    select * from products where price > 50 and name != 'sweatshirt' and price < 75;
} {6|shorts|70.0}

do_execsql_test where-multiple-or {
    select * from products where price > 75 or name = 'shirt' or name = 'coat';
} {1|hat|79.0
2|cap|82.0
3|shirt|18.0
7|jeans|78.0
8|sneakers|82.0
10|coat|33.0
11|accessories|81.0}

do_execsql_test where_in_list {
    select * from products where name in ('hat', 'sweatshirt', 'shorts');
} {1|hat|79.0
5|sweatshirt|74.0
6|shorts|70.0}

do_execsql_test where_not_in_list {
    select * from products where name not in ('hat', 'sweatshirt', 'shorts');
} {2|cap|82.0
3|shirt|18.0
4|sweater|25.0
7|jeans|78.0
8|sneakers|82.0
9|boots|1.0
10|coat|33.0
11|accessories|81.0}

do_execsql_test where_in_list_or_another_list {
    select * from products where name in ('hat', 'sweatshirt', 'shorts') or price in (81.0, 82.0);
} {1|hat|79.0
2|cap|82.0
5|sweatshirt|74.0
6|shorts|70.0
8|sneakers|82.0
11|accessories|81.0}

do_execsql_test where_not_in_list_and_not_in_another_list {
    select * from products where name not in ('hat', 'sweatshirt', 'shorts') and price not in (81.0, 82.0, 78.0, 1.0, 33.0);
} {3|shirt|18.0
4|sweater|25.0}

do_execsql_test where_in_list_or_not_in_another_list {
    select * from products where name in ('hat', 'sweatshirt', 'shorts') or price not in (82.0, 18.0, 78.0, 33.0, 81.0);
} {1|hat|79.0
4|sweater|25.0
5|sweatshirt|74.0
6|shorts|70.0
9|boots|1.0}

do_execsql_test where_in_empty_list {
    select * from products where name in ();
} {}

do_execsql_test where_not_in_empty_list {
    select * from products where name not in ();
} {1|hat|79.0
2|cap|82.0
3|shirt|18.0
4|sweater|25.0
5|sweatshirt|74.0
6|shorts|70.0
7|jeans|78.0
8|sneakers|82.0
9|boots|1.0
10|coat|33.0
11|accessories|81.0}

do_execsql_test where_name_in_list_and_price_gt_70_or_name_exactly_boots {
    select * from products where name in ('hat', 'sweatshirt', 'shorts') and price > 70 or name = 'boots';
} {1|hat|79.0
5|sweatshirt|74.0
9|boots|1.0}

do_execsql_test where_name_in_list_or_price_gt_70_and_name_like_shirt {
    select * from products where name in ('hat', 'shorts') or price > 70 and name like '%shirt%';
} {1|hat|79.0
5|sweatshirt|74.0
6|shorts|70.0}

do_execsql_test where_name_not_in_list_or_name_eq_shirt {
    select * from products where name not in ('shirt', 'boots') or name = 'shirt';
} {1|hat|79.0
2|cap|82.0
3|shirt|18.0
4|sweater|25.0
5|sweatshirt|74.0
6|shorts|70.0
7|jeans|78.0
8|sneakers|82.0
10|coat|33.0
11|accessories|81.0}

do_execsql_test where_multiple {
    select id, first_name, age from users where id = 5 and age < 50;
} {5|Edward|15}

do_execsql_test where_multiple_flipped {
    select id, first_name, age from users where age < 50 and id = 5;
} {5|Edward|15}

do_execsql_test where-parentheses-and {
    select id, name from products where (id = 5 and name = 'sweatshirt') and (id = 5 and name = 'sweatshirt') ORDER BY id;
} {5|sweatshirt}

do_execsql_test where-nested-parentheses {
    select id, name from products where ((id = 5 and name = 'sweatshirt') or (id = 1 and name = 'hat')) ORDER BY id;
} {1|hat
5|sweatshirt}

do_execsql_test where-complex-parentheses {
    select id, name from products where ((id = 5 and name = 'sweatshirt') or (id = 1 and name = 'hat')) and (name = 'sweatshirt' or name = 'hat') ORDER BY id;
} {1|hat
5|sweatshirt}

# regression test for primary key index behavior
do_execsql_test where-id-index-seek-regression-test {
    select id from users where id > 9995;
} {9996
9997
9998
9999
10000}

do_execsql_test where-id-index-seek-regression-test-2 {
    select count(1) from users where id > 0;
} {10000}

# regression test for secondary index (users.age) behavior
do_execsql_test where-age-index-seek-regression-test {
    select age from users where age >= 100 limit 20;
} {100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100}

do_execsql_test where-age-index-seek-regression-test-2 {
    select count(1) from users where age > 0;
} {10000}

do_execsql_test where-simple-between {
    SELECT * FROM products WHERE price BETWEEN 70 AND 100;
} {1|hat|79.0
2|cap|82.0
5|sweatshirt|74.0
6|shorts|70.0
7|jeans|78.0
8|sneakers|82.0
11|accessories|81.0}

do_execsql_test between-price-range-with-names {
    SELECT * FROM products 
    WHERE (price BETWEEN 70 AND 100) 
    AND (name = 'sweatshirt' OR name = 'sneakers');
} {5|sweatshirt|74.0
8|sneakers|82.0}
